<?php

/**
 * Delivery Request Head
 */
class DeRH extends AppModel {

    var $name = 'DeRH';
    var $displayField = 'request_no';
    var $useTable = 'delivery_request_heads';
    
    var $hasOne = array(
        'DeRtH'
    );
    
    var $belongsTo = array(
        'Supplier', 'Customer', 'Site',
        'DeRStatus' => array(
            'foreignKey' => 'delivery_request_status_id'
        ),
        'User'
    );
    var $hasMany = array(
        'DeR' => array(
            'dependent' => true
        )
    );
    var $validate = array(
        'request_no' => array(
            'rule' => array('notEmpty'),
            'message' => 'Request no. is required.',
            'required' => true,
            'on' => 'create'
        ),
        'request_date' => array(
            'rule' => array('notEmpty'),
            'message' => 'Request date is required.',
            'required' => true,
            'on' => 'create'
        ),
        'request_by' => array(
            'rule' => 'notEmpty',
            'message' => 'Request by is required.',
            'required' => true,
            'on' => 'create'
        ),
        'supplier_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Supplier is required.',
            'required' => true,
            'on' => 'create'
        ),
        'customer_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Customer is required.',
            'required' => true,
            'on' => 'create'
        ),
        'site_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Site is required.',
            'required' => true,
            'on' => 'create'
        ),
        'delivery_request_status_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Status is required.',
            'required' => true,
            'on' => 'create'
        ),
        'due_date' => array(
            'rule' => 'notEmpty',
            'message' => 'Due date is required.',
            'required' => true,
            'on' => 'create'
        ),
    );

    function beforeSave() {
        if (isset($this->data['DeRH']['request_date'])) {
            $this->data['DeRH']['request_date'] = $this->dateFormatBeforeSave($this->data['DeRH']['request_date']);
        }
        if (isset($this->data['DeRH']['due_date'])) {
            $this->data['DeRH']['due_date'] = $this->dateFormatBeforeSave($this->data['DeRH']['due_date']);
        }
        if (isset($this->data['DeRH']['supplier_due_date'])) {
            $this->data['DeRH']['supplier_due_date'] = $this->dateFormatBeforeSave($this->data['DeRH']['supplier_due_date']);
        }
        return true;
    }

    function afterFind($results) {
        foreach ($results as &$DeRH) {
            if (isset($DeRH['DeRH'])) {
                if (isset($DeRH['DeRH']['request_date'])) {
                    $DeRH['DeRH']['request_date'] = $this->dateFormatAfterFind($DeRH['DeRH']['request_date']);
                }
                if (isset($DeRH['DeRH']['due_date'])) {
                    $DeRH['DeRH']['due_date'] = $this->dateFormatAfterFind($DeRH['DeRH']['due_date']);
                }
                if (isset($DeRH['DeRH']['supplier_due_date'])) {
                    $DeRH['DeRH']['supplier_due_date'] = $this->dateFormatAfterFind($DeRH['DeRH']['supplier_due_date']);
                }
            }
        }
        return $results;
    }

}

?>
